package com.atguigu.eduservice.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.*;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.*;
import com.atguigu.eduservice.service.slave_dataserver.QianchengItemsQuoteService;
import com.atguigu.eduservice.service.slave_dataserver.QianchengItemsService;
import com.atguigu.eduservice.utils.ExcelCommon;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-02-24
 */
@Api(description="主从数据库")
@RestController
@RequestMapping("/eduservice/mast_slave")
@CrossOrigin
public class Mast_Slavedata_show {

    //访问地址： http://localhost:8001/eduservice/teacher/findAll
    //把service注入
    @Autowired
    private EduTeacherService teacherService;
    @Autowired
    private  StudentService studentService;
    @Autowired
    private MainStudentService mainStudentService;



    @Autowired
    private TbDcUserService tbDcUserService;
    @Autowired
    private TbDcUserOriginService tbDcUserOriginService;

    @Autowired
    private
    QianchengItemsQuoteService qianchengItemsQuoteService;

    @Autowired
    private
    QianchengItemsService qianchengItemsService;

    @Autowired
    private ItemsService itemsService;

    @Autowired
    private ItemsQuoteService itemsQuoteService;

    @ApiOperation(value = "公司产品导入")
    @PostMapping("item_import")
    public R item_import() {
        QueryWrapper<Items> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.select("top 10 * ");
//        wrapperVideo.eq("itemno","F020123F18")
        wrapperVideo.ge("Created","2023-01-01");
        wrapperVideo.orderByDesc("serialid");
        List<Items> itemslist=qianchengItemsService.list(wrapperVideo);
        QueryWrapper<ItemsQuote> wrapper_sub = new QueryWrapper<>();
        Integer ins_count=0;
        Integer update_count=0;
        for (int i=0;i<itemslist.size();i++)
        {
            Items item=itemslist.get(i);
            QueryWrapper<Items> wrapper_item = new QueryWrapper<>();
            wrapper_item.eq("itemno",item.getItemNo());
            Integer count=itemsService.count(wrapper_item);
            if (count==0){
                itemsService.save(item);
                ins_count++;}

            else {
                itemsService.update(item, wrapper_item);
                update_count++;
            }


               // if (itemsService.save(item)){

                    wrapper_sub = new QueryWrapper<>();
                    wrapper_sub.eq("parentid",item.getRecordID());

                    List<ItemsQuote>itemsQuotelist=qianchengItemsQuoteService.list(wrapper_sub);
                    if (itemsQuotelist.size()>0){


                       itemsQuoteService.remove(wrapper_sub);
                       for (int j=0;j<itemsQuotelist.size();j++)
                        {
                           ItemsQuote itemsQuote=itemsQuotelist.get(j);
                           itemsQuoteService.save(itemsQuote);
                        }


                      }





        }
        itemslist=itemsService.list(wrapperVideo);

        for (int i=0;i<itemslist.size();i++) {
            Items item = itemslist.get(i);
            QueryWrapper<ItemsQuote> wrapper = new QueryWrapper<>();
            wrapper.eq("parentid", item.getRecordID());
            List<ItemsQuote> itemsQuotelist = itemsQuoteService.list(wrapper);
            item.setItemsQuotelist(itemsQuotelist);
        }

        return R.ok().data("ins_count",ins_count).data("update_count",update_count).data("itemlist",itemslist);


    }








    @ApiOperation(value = "学生列表主从复制")
    @PostMapping("student_save")
    public R student_findAll() {
        //调用service的方法实现查询所有的操作
        List<Tstudent> liststudent = studentService.list(null);
        Integer hj=0;
        String name="";

        for (int i = 0; i < liststudent.size(); i++) {
            QueryWrapper<Tstudent> wrapperVideo = new QueryWrapper<>();
            wrapperVideo.eq("name", liststudent.get(i).getName());

            Integer count = mainStudentService.count(wrapperVideo);
            if (count == 0) {
                mainStudentService.save(liststudent.get(i));
                hj++;
            if (!StringUtils.isEmpty(name))
                name=name+"/"+liststudent.get(i).getName();
            else
                name=liststudent.get(i).getName();
            }
        }
        List<Tstudent> listmainstudent = mainStudentService.get_studentall();
        return R.ok().data("insert_count",hj).data("insert_name",name).data("items",listmainstudent);
    }


}

